//
//  FPDiscoverViewController.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPDiscoverViewController.h"
#import "FPNewsViewController.h"
#import "FPPrevueViewController.h"
#import "FPTopViewController.h"
#import "FPFilmcriticViewController.h"
#import "FPNavBarView.h"

@interface FPDiscoverViewController ()<UIScrollViewDelegate>
/**
 轮播scrollView
 */
@property(nonatomic,strong)UIScrollView *DLYScrollView;

/**
 自定义当行兰
 */
@property(nonatomic,strong)FPNavBarView *barView;

@end

@implementation FPDiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = YES;
   [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"cgClear"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.backgroundColor = [UIColor purpleColor];
   UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , 20)];
    statusBarView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:statusBarView];
    [self setNavigationButton];
    [self creatScrollView];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ================自定义导航栏按钮
- (void)setNavigationButton
{
    self.barView = [[FPNavBarView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.navigationController.navigationBar.frame.size.height)];
  //  self.barView.backgroundColor = [UIColor purpleColor];
    [self.barView addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = _barView;
}
//实现方法
- (void)clickButtonAction:(UIButton *)sender
{
    switch (sender.tag) {
        case 100:
            [self newsButton];
         
            break;
        case 101:
            [self prevueButton];
            break;
        case 102:
            [self topButton];
            break;
        case 103:
            [self filmCriticButton];
            break;
        default:
            break;
    }
}



//点击新闻按钮
- (void)newsButton
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    CGRect frame = self.barView.lineView.frame;
    frame.origin.x = -10;
    self.barView.lineView.frame = frame;
    [UIView commitAnimations];
    //改变scrollVie的偏移量
    self.DLYScrollView.contentOffset = CGPointMake(0, 0);
    
}


//预告片按钮
- (void)prevueButton
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    CGRect frame = self.barView.lineView.frame;
    frame.origin.x = kScreenWidth/4 + kImageW(-10);
    self.barView.lineView.frame = frame;
     [UIView commitAnimations];
    //改变scrollVie的偏移量
    self.DLYScrollView.contentOffset = CGPointMake(kScreenWidth, 0);
    
}
//排行榜按钮
- (void)topButton
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    CGRect frame = self.barView.lineView.frame;
    frame.origin.x = kImageW(-10) + kScreenWidth/4 * 2;
    self.barView.lineView.frame = frame;
    [UIView commitAnimations];
    //改变scrollVie的偏移量
    self.DLYScrollView.contentOffset = CGPointMake(kScreenWidth * 2, 0);

}
//影评按钮
- (void)filmCriticButton
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    CGRect frame = self.barView.lineView.frame;
    frame.origin.x =  kImageW(-10) +kScreenWidth/4 * 3;
    self.barView.lineView.frame = frame;
    [UIView commitAnimations];
    //改变scrollVie的偏移量
    self.DLYScrollView.contentOffset = CGPointMake(kScreenWidth * 3, 0);

}
#pragma mark=================添加子控制器

- (void)creatScrollView
{
    //添加scrollview
      self.DLYScrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.DLYScrollView.delegate = self;
    self.DLYScrollView.contentSize = CGSizeMake(kScreenWidth * 4, 0);
    self.DLYScrollView.contentOffset = CGPointMake(0, 0);
    self.DLYScrollView.pagingEnabled = YES;
    self.DLYScrollView.bounces = NO;
    self.DLYScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_DLYScrollView];
    
   //添加新闻controller
    FPNewsViewController *newsViewController = [[FPNewsViewController alloc]init];
  
    [self addChildViewController:newsViewController];
    [self.DLYScrollView addSubview:newsViewController.view];
    
     //添加预告片controller
    FPPrevueViewController *prevueViewController = [[FPPrevueViewController alloc]init];
    [self addChildViewController:prevueViewController];
    [self.DLYScrollView addSubview:prevueViewController.view];
    //添加排行控制器
    FPTopViewController *topViewController = [[FPTopViewController alloc]init];
    [self addChildViewController:topViewController];
    [self.DLYScrollView addSubview:topViewController.view];

    //添加影评控制器
    FPFilmcriticViewController *filmCriticViewController = [[FPFilmcriticViewController alloc]init];
    [self addChildViewController:filmCriticViewController];
    [self.DLYScrollView addSubview:filmCriticViewController.view];
}
//滑动屏幕改变线条位置
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.DLYScrollView.contentOffset.x == kScreenWidth)
    {
        [self prevueButton];
    }
    else if (self.DLYScrollView.contentOffset.x == 2*kScreenWidth)
    {
        [self topButton];
    }
    else if (self.DLYScrollView.contentOffset.x == 3*kScreenWidth)
    {
        [self filmCriticButton];
    }
    else if(self.DLYScrollView.contentOffset.x == 0)
    {
        [self newsButton];
    }
}

@end
